<template>
  <div id="login-top">
    <div id="ms-login">
      <!--标题-->
      <div id="ms-title">后台管理系统</div>
      <!--用户名-->
      <el-form
        :model="param"
        :rules="rules"
        ref="login"
        label-width="0px"
        id="ms-content"
      >
        <el-form-item prop="userName">
          <el-input v-model="param.userName" placeholder="userName">
            <el-button slot="prepend" icon="el-icon-lx-people"></el-button>
          </el-input>
        </el-form-item>
        <!--密码-->
        <el-form-item prop="password">
          <el-input
            type="password"
            placeholder="password"
            v-model="param.password"
            @keyup.enter.native="submitForm()"
          >
            <el-button slot="prepend" icon="el-icon-lx-lock"></el-button>
          </el-input>
        </el-form-item>
        <!--登录-->
        <div id="login-btn">
          <el-button type="primary" @click="submitForm()">登录</el-button>
        </div>
        <!--备注-->
        <p id="login-tips">如果登录失败，不要哭，专业写BUG团队！</p>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data: function() {
    return {
      param: {
        userName: "admin",
        password: "tiger",
      },
      rules: {
        userName: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm() {
      var _this = this;
      //清空本地存储的token
      localStorage.setItem("token", "");
      //用户登录
      _this.$http
        .get(
          //地址
          "/api/sso/userLogin/authUser?userName=" +
            _this.param.userName +
            "&passWord=" +
            _this.param.password
        )
        .then(function(response) {
          console.log(response);
          if (response.data.code == 2000) {
            _this.$message.success("登录成功！");
            // alert(JSON.stringify(response.data));
            localStorage.setItem("ms_username", _this.param.userName);
            console.log(_this.param.userName);
            localStorage.setItem("token", response.data.data.accessToken);
            localStorage.setItem("userId", response.data.data.user.userId);
            localStorage.setItem("userName", response.data.data.user.userName);
            localStorage.setItem("phonenumber", response.data.data.user.phonenumber);
            localStorage.setItem("email", response.data.data.user.email);
            localStorage.setItem("avatar", response.data.data.user.avatar);
            localStorage.setItem("remark", response.data.data.user.remark);
            localStorage.setItem(
              "loginName",
              response.data.data.user.loginName
            );
            localStorage.setItem("avatar", response.data.data.user.avatar);
            // console.log(response.data + ".....555555555.......");
            _this.$router.push("/index");
            //location.reload();
          } else {
            _this.$message.error("用户名或者密码错误");
          }
        })
        .catch(function(error) {
          console.log(error);
          _this.$message.error("你错了。。。");
        });
    },
  },
};
</script>

<style>
#login-top {
  position: relative;
  width: 100%;
  height: 700px;
  /* background-color: aqua; */
  background-image: url(../assets/images/loginBg.jpg);
  background-size: 100%;
}
#ms-title {
  width: 100%;
  line-height: 50px;
  text-align: center;
  font-size: 20px;
  color: rgb(7, 7, 7);
  border-bottom: 1px solid #ddd;
}
#ms-login {
  position: absolute;
  left: 50%;
  top: 350px;
  width: 350px;
  margin: -190px 0 0 -175px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.3);
  overflow: hidden;
}
#ms-content {
  padding: 30px 30px;
}
#login-btn {
  text-align: center;
}
#login-btn button {
  width: 100%;
  height: 36px;
  margin-bottom: 10px;
}
#login-tips {
  font-size: 12px;
  line-height: 30px;
  color: rgb(8, 8, 8);
}
</style>
